package cc.ccoke.algorithms.util;

import java.util.Random;

/**
 * 数组工具类
 * @author ccoke
 */
public class ArrayUtils {

    /**
     * 生成随机数组
     * @param maxSize 最大长度
     * @param maxNumber 生成元素的最大值
     * @return 随机数组
     */
    public static int[] generateByInt(int maxSize, int maxNumber) {
        Random rand =new Random(System.currentTimeMillis());
        int arraySize = rand.nextInt(maxSize) + 1;
        int[] arr = new int[arraySize];
        for (int i = 0; i < arraySize; i++) {
            arr[i] = rand.nextInt(maxNumber) - rand.nextInt(maxNumber);
        }
        return arr;
    }

    /**
     * 为两个数交换空间
     * @param m
     * @param n
     */
    public static void swap(int[] arr, int m, int n) {
        int temp = arr[m];
        arr[m] = arr[n];
        arr[n] = temp;
    }

    /**
     * 打印数组
     * @param arr 数组
     */
    public static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + (i == arr.length - 1 ? "\n" : ","));
        }
    }
}
